<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>闭包做选项卡</title>
	<style type="text/css">
		.btns{
			width: 500px;
			height: 50px;
		}

		.btns input{
			width: 100px;
			height: 50px;
			background-color: #ddd;
			color: #666;
			border: 0px;
		}

		.btns input .cur{
			background-color: gold;
		}

		.contents div{
			width: 500px;
			height: 300px;
			background-color: gold;
			display: none;
			line-height: 300px;
			text-align: center;
		}
	
		.contents div.active{
			display: block;
		}
	</style>

	<script type="text/javascript">
		window.onload = function(){
			var aBtn = document.getElementById('btns').getElementsByTagName('input');

			// alert(aBtn.length);
			
			var aCon = document.getElementById('contents').getElementsByTagName('div');

			for(var i=0;i<aBtn.length;i++){

				(function(i){

					aBtn[i].onclick = function(){
					
					for(var j=0;j<aBtn.length;j++){
						// 注意需要先把样式清除
						aBtn[j].className = '';
						aCon[j].className = '';
					}
					this.className = 'cur';

					// 理由是这里i存不了值,循环之后,i就等于3,所以要使用闭包
					aCon[i].className = 'active';
				}

				})(i);
				
				}
			}
		}
	</script>
</head>
<body>
	<div class="btns" id="btns">
		<input type="button" value="tab01" class="cur">
		<input type="button" value="tab02">
		<input type="button" value="tab03">
	</div>
	<div class="contents" id="contents">
		<div class="active">tab文字内容1</div>
		<div>tab文字内容2</div>
		<div>tab文字内容3</div>
	</div>
</body>
</html>